/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package database.entitymanager;

import database.entity.ShopImage;
import database.entitymanager.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import database.entity.ShopItem;

/**
 *
 * @author Stefan
 */
public class ShopImageManager {

    private static EntityManagerFactory emf = EntityFactoryHolder.getInstance();

    public static EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public static void create(ShopImage shopImage) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ShopItem shopItem = shopImage.getShopItem();
            if (shopItem != null) {
                shopItem = em.getReference(shopItem.getClass(), shopItem.getItemid());
                shopImage.setShopItem(shopItem);
            }
            em.persist(shopImage);
            if (shopItem != null) {
                shopItem.getShopImageList().add(shopImage);
                shopItem = em.merge(shopItem);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void edit(ShopImage shopImage) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ShopImage persistentShopImage = em.find(ShopImage.class, shopImage.getShopimageid());
            ShopItem shopItemOld = persistentShopImage.getShopItem();
            ShopItem shopItemNew = shopImage.getShopItem();
            if (shopItemNew != null) {
                shopItemNew = em.getReference(shopItemNew.getClass(), shopItemNew.getItemid());
                shopImage.setShopItem(shopItemNew);
            }
            shopImage = em.merge(shopImage);
            if (shopItemOld != null && !shopItemOld.equals(shopItemNew)) {
                shopItemOld.getShopImageList().remove(shopImage);
                shopItemOld = em.merge(shopItemOld);
            }
            if (shopItemNew != null && !shopItemNew.equals(shopItemOld)) {
                shopItemNew.getShopImageList().add(shopImage);
                shopItemNew = em.merge(shopItemNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = shopImage.getShopimageid();
                if (findShopImage(id) == null) {
                    throw new NonexistentEntityException("The shopImage with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ShopImage shopImage;
            try {
                shopImage = em.getReference(ShopImage.class, id);
                shopImage.getShopimageid();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The shopImage with id " + id + " no longer exists.", enfe);
            }
            ShopItem shopItem = shopImage.getShopItem();
            if (shopItem != null) {
                shopItem.getShopImageList().remove(shopImage);
                shopItem = em.merge(shopItem);
            }
            em.remove(shopImage);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public static List<ShopImage> findShopImageEntities() {
        return findShopImageEntities(true, -1, -1);
    }

    public static List<ShopImage> findShopImageEntities(int maxResults, int firstResult) {
        return findShopImageEntities(false, maxResults, firstResult);
    }

    private static List<ShopImage> findShopImageEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ShopImage.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public static ShopImage findShopImage(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ShopImage.class, id);
        } finally {
            em.close();
        }
    }

    public static int getShopImageCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ShopImage> rt = cq.from(ShopImage.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
